<?php

namespace Action;
use Library\User\Session;

class Results extends \Library\Action\PortalAction {

	private $_userResults;
	private $_options;

	public function execute() {
		parent::execute();
		
		if( !_u() ) {
			throw new \Exception( 'You must be authenticated to access results page' );
		}
		
		$this->setTitle( 'Results page' );
		
	}
	
	public function getSearchBar() {
		if( $this->_searchBar === null ) {
			$this->_searchBar = new \Library\Widget\SearchBar( _u() );
		}
		return $this->_searchBar;
	}
	
	public function getSearchMessage() {
		return trim($this->getParam( 'search' ));
	}
	
	public function getUserResults() {
		if( $this->_userResults === null ) {
		
			$search = $this->getSearchMessage();
			if( !$search ) {
				return $this->_userResults = array();
			}
		
			$fullName = 'CONCAT(first_name, CONCAT( \' \', last_name))';
		
			$results = _q()->searchN( 'users',
				array( 
					'first_name',
					'last_name',
					$fullName
				),
				$search, $fullName, 0, 5 );
			foreach( $results as $result ) {
				$user = \Bean\User::getUser( $result );
				$this->_userResults[] = new \Library\Widget\Result\UserResult( $user );
			}
		}
		return $this->_userResults;
	}
	
	public function getOptions() {
		if( $this->_options === null ) {
			$this->_options = new \Library\Widget\Options();
		}
		return $this->_options;
	}
}
